package net.xdclass.service.impl;

import net.xdclass.enums.DomainTypeEnum;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.vo.DomainVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.model.DomainDO;
import net.xdclass.mapper.DomainMapper;
import net.xdclass.service.DomainService;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/11 21:39
 */
@Service
public class DomainServiceImpl extends ServiceImpl<DomainMapper, DomainDO> implements DomainService {


    /**
     * 查找详情
     *
     * @param id
     * @param accountNO
     * @return
     */
    @Override
    public DomainDO findById(Long id, Long accountNO) {

        return this.lambdaQuery()
                .eq(DomainDO::getId, id)
                .eq(DomainDO::getAccountNo, accountNO)
                .one();
    }

    /**
     * 查找详情
     *
     * @param id
     * @param domainTypeEnum
     * @return
     */
    @Override
    public DomainDO findByDomainTypeAndID(Long id, DomainTypeEnum domainTypeEnum) {

        return this.lambdaQuery()
                .eq(DomainDO::getId, id)
                .eq(DomainDO::getDomainType, domainTypeEnum.name())
                .one();

    }

    /**
     * 新增
     *
     * @param domainDO
     * @return
     */
    @Override
    public int addDomain(DomainDO domainDO) {
        return this.getBaseMapper().insert(domainDO);
    }

    /**
     * 列举全部官方域名
     *
     * @return
     */
    @Override
    public List<DomainDO> listOfficialDomain() {

        return this.lambdaQuery()
                .eq(DomainDO::getDomainType, DomainTypeEnum.OFFICIAL.name())
                .list();
    }

    /**
     * 列举全部自定义域名
     *
     * @param accountNo
     * @return
     */
    @Override
    public List<DomainDO> listCustomDomain(Long accountNo) {
        return this.lambdaQuery()
                .eq(DomainDO::getDomainType, DomainTypeEnum.CUSTOM.name())
                .list();
    }

    @Override
    public List<DomainVO> listAll() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        List<DomainDO> customDomainList = listCustomDomain(accountNo);
        List<DomainDO> officialDomainList = listOfficialDomain();

        customDomainList.addAll(officialDomainList);

        return customDomainList.stream().map(obj-> beanProcess(obj)).collect(Collectors.toList());
    }


    private DomainVO beanProcess(DomainDO domainDO){

        DomainVO domainVO = new DomainVO();

        BeanUtils.copyProperties(domainDO,domainVO);

        return domainVO;

    }
}
